<?php

namespace app\api\exception;

use app\admin\controller\AdminBase;
use app\common\lib\Status;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;

class Http extends Handle
{
    protected $httpStatus = 501;

    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request   $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response
    {
        if ($e instanceof HttpResponseException) {
            //
            return parent::render($request, $e);
        }

        // 添加自定义异常处理机制
        if (method_exists($e, 'getStatusCode')) {
            $httpStatus = $e->getStatusCode();
        } else {
            $httpStatus = $this->httpStatus;
        }
        return Status::show(config('status.error'), $e->getMessage());

        // 其他错误交给系统处理
        return parent::render($request, $e);
    }
}
